//
//  FilterSpecilCharViewController.m
//  20170207
//
//  Created by xuPeng on 2017/9/6.
//  Copyright © 2017年 陈旭鹏. All rights reserved.
//

#import "FilterSpecilCharViewController.h"
#import "Header.h"

@interface FilterSpecilCharViewController ()<UITextFieldDelegate>
@property (nonatomic , strong) UITextField *textField;
@property (nonatomic , strong) NSString *str;
@end

/*
 // 定义一个特殊字符的集合
 NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:
 @"@／：；（）¥「」＂、[]{}#%-*+=_\\|~＜＞$€^•'@#$%^&*()_+'\""];
 // 过滤字符串的特殊字符
 NSString *newString = [trimString stringByTrimmingCharactersInSet:set];
 
 */


@implementation FilterSpecilCharViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.title = @"过滤特殊字符";
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, PW-40, 40)];
//    self.textField.center = self.view.center;
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.placeholder = @"请输入内容";
    self.textField.font = [UIFont fontWithName:@"Arial" size:14.0f];
    self.textField.clearButtonMode = UITextFieldViewModeAlways;
    self.textField.text = @"";
    self.textField.textAlignment = NSTextAlignmentLeft;
    
    // 设置自动缩小显示的最小字体，才可以使长文滑动
    self.textField.adjustsFontSizeToFitWidth = YES;
    self.textField.minimumFontSize = 20;
    
    self.textField.returnKeyType = UIReturnKeyDone;
    
    // 键盘外观 深灰  一般默认为浅灰色
    self.textField.keyboardAppearance = UIKeyboardAppearanceAlert;
    
    self.textField.delegate = self;
    [self.view addSubview:self.textField];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [self.textField resignFirstResponder];
    self.str = self.textField.text;
    
    // 定义一个特殊字符的集合    *这里的特殊字符是英文字符*
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@／：；（）¥「」＂、[]{}#%-*+=_\\|~＜＞$€^•'@#$%^&*()_+'\""];
    // 过滤字符串的特殊字符
    NSString *newString = [self.str stringByTrimmingCharactersInSet:set];
    
    if ([newString isEqualToString:self.str]) {
    }else{
    
        UIAlertView * alert =[[UIAlertView alloc]initWithTitle:@"tip" message:@"包含有特殊符号" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alert show];
        
    }
    return YES;
   
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    self.str = self.textField.text;
    
    // 定义一个特殊字符的集合   *这里的特殊字符是英文字符*
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@／：；；？“”（）¥「」＂、[]{}#%-*+=_\\|~＜＞$€^•'@#$%^&*()_+'\""];
    // 过滤字符串的特殊字符
    NSString *newString = [self.str stringByTrimmingCharactersInSet:set];
    
    if ([newString isEqualToString:self.str]) {
    }else{
        
        UIAlertView * alert =[[UIAlertView alloc]initWithTitle:@"tip" message:@"包含有特殊符号" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
        [alert show];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

        return YES;
}

@end
